#include "widget.h"
#include "ui_widget.h"
#include "zhuce.h"
#include "putongui.h"
Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    socket = new QTcpSocket(this); //创建父类套接字
    //zhuce = new zhuce(this);
    zhuceShow = new zhuce; //注册申请
    //普通用户界面
    putongShow = new puTongUI;
    message = new messageText; //申请传输信息包
    QString ip = "192.168.5.60";
    QString port = "7776";
    socket->connectToHost(QHostAddress(ip),port.toShort());
    connect(socket,&QTcpSocket::connected,[=]{QMessageBox::information(this,"connect is ok","connect ok");}); //判断是否链接成功
    connect(socket,&QTcpSocket::readyRead,[=]{socket->read((char *)message,(qint64)sizeof(messageText));});//将数据放到message结构体中
    connect(socket,&QTcpSocket::disconnected,[=]{QMessageBox::warning(this,"connect error","error");});
    connect(zhuceShow,&zhuce::zhujiemianxianshi,[=]{this->show();zhuceShow->close();}); //从注册界面返回登入界面
    connect(putongShow,&puTongUI::puTong_zhujiemianShow,[=]{this->show();putongShow->close();});  //  给注册消息传输socket
    connect(this,SIGNAL(transmission(messageText *,QTcpSocket *)),zhuceShow,SLOT(needMessageAndSocket(messageText *,QTcpSocket *))); //给注册界面传输message和socket
    connect(this,SIGNAL( transmissionPuTong(messageText *,QTcpSocket *)),putongShow,SLOT(puTong_NeedMessage(messageText*,QTcpSocket*))); //给普通用户界面传输message和socket
}
Widget::~Widget()
{
    delete ui;
}
//登入处理
void Widget::on_dengru_clicked()
{
    message->id = ui->zhanghao->text().toInt();
    message->passwd = ui->mima->text().toInt();
    message->flags = DENGRU;
    qDebug() << message->id;
    qDebug() << message->passwd;
//发送数据
    socket->write((char *)message,(qint64)sizeof(messageText));
//读数据
    connect(socket,&QTcpSocket::readyRead,[=]{socket->read((char *)message,(qint64)sizeof(messageText));});//将数据放到message结构体中
    qDebug() << message->mod;
    switch (message->mod) {
    case SUPER:
        //跳转超级用户界面
        qDebug() << message->id;
        qDebug() << message->passwd;
            //
        break;
    case ORDINARY:
        //跳转普通用户界面
        qDebug() << message->id;
        qDebug() << message->passwd;
        qDebug() << message->ret;
        qDebug() << message->mod;
        qDebug() << message->solary;
        qDebug() << message->flags;
        this->hide(); //登入界面隐藏
        putongShow->show(); //普通用户界面打开
        emit transmissionPuTong(this->message,this->socket);
        break;
    }
}
//注册处理
void Widget::on_zhuce_clicked()
{
    //注册界面
    this->hide();
    zhuceShow->show();
//将message发送到
    emit transmission(message,socket);
}
